package com.migo.controller;

import com.migo.entity.FirmwareZipEntity;
import com.migo.service.FirmwareZipService;
import com.migo.utils.FileMD5Util;
import com.migo.utils.PageUtils;
import com.migo.utils.Query;
import com.migo.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.net.MalformedURLException;
import java.util.List;
import java.util.Map;


/**
 * zip固件信息表
 * 
 * @author powervision
 * @email wes.wang@powervision.me
 * @date 2020-04-21 10:06:19
 */
@RestController
@RequestMapping("firmwarezip")
public class FirmwareZipController {

	@Autowired
	private FirmwareZipService firmwareZipService;



	@Value("${ossHttpUrl}")
	String ossHttpUrl;
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("firmwarezip:list")
	public R list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<FirmwareZipEntity> firmwareZipList = firmwareZipService.queryList(query);
		int total = firmwareZipService.queryTotal(query);
		
		PageUtils pageUtil = new PageUtils(firmwareZipList, total, query.getLimit(), query.getPage());
		
		return R.ok().put("page", pageUtil);
	}
	
	
	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("firmwarezip:info")
	public R info(@PathVariable("id") Integer id){
		FirmwareZipEntity firmwareZip = firmwareZipService.queryObject(id);
		
		return R.ok().put("firmwareZip", firmwareZip);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("firmwarezip:save")
	public R save(@RequestBody FirmwareZipEntity firmwareZip){
		//更新文件大小
		try {
			String url  = ossHttpUrl+firmwareZip.getFirmwareDownloadAddr();
			firmwareZip.setFirmwareSize(FileMD5Util.getFileSize(url));
		} catch (MalformedURLException e) {
			e.printStackTrace();
		}
		firmwareZipService.save(firmwareZip);
		return R.ok();
	}
	
	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("firmwarezip:update")
	public R update(@RequestBody FirmwareZipEntity firmwareZip){
		firmwareZipService.update(firmwareZip);
		
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("firmwarezip:delete")
	public R delete(@RequestBody Integer[] ids){
		firmwareZipService.deleteBatch(ids);
		
		return R.ok();
	}
	
}
